package com.zjz.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {


    //Chain: 链
    /*
        1.过滤器中所有代码，在过滤特定请求的时候都会被执行
        2.必须要让过滤器继续通行  filterChain.doFilter(request,response);
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("CharacterEncodingFilter执行前：");
        filterChain.doFilter(request,response);//让我们的代码继续走，如果不写，程序到这就会被拦截
        System.out.println("CharacterEncodingFilter执行后：");

    }

    //初始化    ，web服务器启动时就初始化，因为它要随时监听，随时等待过滤对象出现
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter开始初始化：");
    }

    //销毁 web服务器关闭销毁
    @Override
    public void destroy() {
        System.out.println("CharacterEncodingFilter销毁：");
    }
}
